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