2007-04-20 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-connection-specific-smtp-edit-window.c
1 /* connection-specific-smtp-window.c */
2
3 #include "modest-connection-specific-smtp-edit-window.h"
4 #include <hildon-widgets/hildon-caption.h>
5 #include "widgets/modest-serversecurity-combo-box.h"
6 #include "widgets/modest-secureauth-combo-box.h"
7 #include "widgets/modest-validating-entry.h"
8 #include <modest-account-mgr-helpers.h>
9 #include <gtk/gtkbutton.h>
10 #include <gtk/gtkhbox.h>
11 #include <gtk/gtkvbox.h>
12 #include <gtk/gtkstock.h>
13
14 #include <glib/gi18n.h>
15
16 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
17
18 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
19         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
20
21 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
22
23 struct _ModestConnectionSpecificSmtpEditWindowPrivate
24 {
25         GtkWidget *entry_outgoingserver;
26         GtkWidget *combo_outgoing_auth;
27         GtkWidget *entry_user_username;
28         GtkWidget *entry_user_password;
29         GtkWidget *combo_outgoing_security;
30         GtkWidget *entry_port;
31         
32         GtkWidget *button_ok;
33         GtkWidget *button_cancel;
34 };
35
36 static void
37 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
38                                                                                                                         GValue *value, GParamSpec *pspec)
39 {
40         switch (property_id) {
41         default:
42                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
43         }
44 }
45
46 static void
47 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
48                                                                                                                         const GValue *value, GParamSpec *pspec)
49 {
50         switch (property_id) {
51         default:
52                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53         }
54 }
55
56 static void
57 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
58 {
59         if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
60                 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
61 }
62
63 static void
64 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
65 {
66         G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
67 }
68
69 static void
70 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
71 {
72         GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
75
76         object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
77         object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
78         object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
79         object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
80 }
81
82 enum MODEL_COLS {
83         MODEL_COL_NAME = 0,
84         MODEL_COL_SERVER_NAME = 1,
85         MODEL_COL_ID = 2
86 };
87
88 static void
89 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
90 {
91         ModestConnectionSpecificSmtpEditWindow *self = 
92                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
93         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
94                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
95         
96         const gint port_number = 
97                 modest_serversecurity_combo_box_get_active_serversecurity_port (
98                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
99
100         if(port_number != 0) {
101                 gchar* str = g_strdup_printf ("%d", port_number);
102                 gtk_entry_set_text (GTK_ENTRY (priv->entry_port), str);
103                 g_free (str);   
104         }               
105 }
106
107 static void
108 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
109 {
110         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
111                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
112         
113         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, 2); */
114         
115         /* Create a size group to be used by all captions.
116          * Note that HildonCaption does not create a default size group if we do not specify one.
117          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
118         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
119          
120         /* The outgoing server widgets: */
121         if (!priv->entry_outgoingserver)
122                 priv->entry_outgoingserver = gtk_entry_new ();
123         GtkWidget *caption = hildon_caption_new (sizegroup, 
124                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
125         gtk_widget_show (priv->entry_outgoingserver);
126         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
127         gtk_widget_show (caption);
128         
129         /* Show a default port number when the security method changes, as per the UI spec: */
130         g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
131         
132         
133         /* The secure authentication widgets: */
134         if (!priv->combo_outgoing_auth)
135                 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
136         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
137                 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
138         gtk_widget_show (priv->combo_outgoing_auth);
139         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
140         gtk_widget_show (caption);
141         
142         /* The username widgets: */     
143         priv->entry_user_username = GTK_WIDGET (easysetup_validating_entry_new ());
144         caption = hildon_caption_new (sizegroup, _("mail_fi_username"), 
145                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
146         gtk_widget_show (priv->entry_user_username);
147         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
148         gtk_widget_show (caption);
149         
150         /* Prevent the use of some characters in the username, 
151          * as required by our UI specification: */
152         easysetup_validating_entry_set_unallowed_characters_whitespace (
153                 EASYSETUP_VALIDATING_ENTRY (priv->entry_user_username));
154         
155         /* Set max length as in the UI spec:
156          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
157         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
158         
159         /* The password widgets: */     
160         priv->entry_user_password = gtk_entry_new ();
161         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
162         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
163         caption = hildon_caption_new (sizegroup, 
164                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
165         gtk_widget_show (priv->entry_user_password);
166         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
167         gtk_widget_show (caption);
168         
169         /* The secure connection widgets: */    
170         if (!priv->combo_outgoing_security)
171                 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
172         modest_serversecurity_combo_box_fill (
173                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
174         modest_serversecurity_combo_box_set_active_serversecurity (
175                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_SECURITY_NONE);
176         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
177                 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
178         gtk_widget_show (priv->combo_outgoing_security);
179         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
180         gtk_widget_show (caption);
181         
182         /* The port number widgets: */
183         if (!priv->entry_port)
184                 priv->entry_port = gtk_entry_new ();
185         caption = hildon_caption_new (sizegroup, 
186                 _("mcen_li_emailsetup_smtp"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
187         gtk_widget_show (priv->entry_port);
188         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, 2);
189         gtk_widget_show (caption);
190         
191         /* Add the buttons: */
192         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
193         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
194         
195         
196         gtk_widget_show (box);
197 }
198
199 ModestConnectionSpecificSmtpEditWindow*
200 modest_connection_specific_smtp_edit_window_new (void)
201 {
202         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
203 }
204
205 void
206 modest_connection_specific_smtp_edit_window_set_connection (
207         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
208         const ModestServerAccountData *data)
209 {
210         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
211                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
212
213         /* This causes a warning because of the %s in the translation, but not in the original string: */
214         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
215         gtk_window_set_title (GTK_WINDOW (window), title);
216         g_free (title);
217         
218         if (data) 
219         {
220                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
221                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);     
222                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
223         
224                 modest_serversecurity_combo_box_set_active_serversecurity (
225                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
226         
227                 modest_secureauth_combo_box_set_active_secureauth (
228                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
229                 
230                 /* port: */
231                 gchar * port_str = g_strdup_printf ("%d", data->port);
232                 gtk_entry_set_text (GTK_ENTRY (priv->entry_port), port_str);
233                 g_free (port_str);
234         }
235 }
236
237 /*
238  * The result must be freed with modest_account_mgr_free_server_account_data(). */
239 ModestServerAccountData*
240 modest_connection_specific_smtp_edit_window_get_settings (
241         ModestConnectionSpecificSmtpEditWindow *window, 
242         ModestAccountMgr *account_manager, const gchar* server_account_name)
243 {
244         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
245                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
246         
247         g_assert (server_account_name);
248         
249         /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data() 
250          * expects us to use. */
251         ModestServerAccountData *result = g_slice_new0 (ModestServerAccountData);
252         
253         result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
254         result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));       
255         result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
256         
257         result->security = modest_serversecurity_combo_box_get_active_serversecurity (
258                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
259         
260         result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
261                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
262                 
263         /* port: */
264         const gchar * port_str = gtk_entry_get_text (GTK_ENTRY (priv->entry_port));
265         if (port_str)
266                 result->port = atoi (port_str);
267                         
268         return result;
269 }