2007-05-15 Murray Cumming <murrayc@murrayc.com>
[modest] / src / widgets / modest-serversecurity-combo-box.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
31 #include "modest-serversecurity-combo-box.h"
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.h>
36
37 #include <stdlib.h>
38 #include <string.h> /* For memcpy() */
39
40 /* Include config.h so that _() works: */
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 G_DEFINE_TYPE (ModestServersecurityComboBox, modest_serversecurity_combo_box, GTK_TYPE_COMBO_BOX);
46
47 #define SERVERSECURITY_COMBO_BOX_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERSECURITY_COMBO_BOX, ModestServersecurityComboBoxPrivate))
49
50 typedef struct _ModestServersecurityComboBoxPrivate ModestServersecurityComboBoxPrivate;
51
52 struct _ModestServersecurityComboBoxPrivate
53 {
54         GtkTreeModel *model;
55         ModestConnectionProtocol protocol;
56 };
57
58 static void
59 modest_serversecurity_combo_box_get_property (GObject *object, guint property_id,
60                                                                                                                         GValue *value, GParamSpec *pspec)
61 {
62         switch (property_id) {
63         default:
64                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
65         }
66 }
67
68 static void
69 modest_serversecurity_combo_box_set_property (GObject *object, guint property_id,
70                                                                                                                         const 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_serversecurity_combo_box_dispose (GObject *object)
80 {
81         if (G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->dispose)
82                 G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->dispose (object);
83 }
84
85 static void
86 modest_serversecurity_combo_box_finalize (GObject *object)
87 {
88         ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
89
90         g_object_unref (G_OBJECT (priv->model));
91
92         G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->finalize (object);
93 }
94
95 static void
96 modest_serversecurity_combo_box_class_init (ModestServersecurityComboBoxClass *klass)
97 {
98         GObjectClass *object_class = G_OBJECT_CLASS (klass);
99
100         g_type_class_add_private (klass, sizeof (ModestServersecurityComboBoxPrivate));
101
102         object_class->get_property = modest_serversecurity_combo_box_get_property;
103         object_class->set_property = modest_serversecurity_combo_box_set_property;
104         object_class->dispose = modest_serversecurity_combo_box_dispose;
105         object_class->finalize = modest_serversecurity_combo_box_finalize;
106 }
107
108 enum MODEL_COLS {
109         MODEL_COL_NAME = 0, /* a string */
110         MODEL_COL_ID = 1 /* an int. */
111 };
112
113 static void
114 modest_serversecurity_combo_box_init (ModestServersecurityComboBox *self)
115 {
116         ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (self);
117
118         /* Create a tree model for the combo box,
119          * with a string for the name, and an ID for the serversecurity.
120          * This must match our MODEL_COLS enum constants.
121          */
122         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
123
124         /* Setup the combo box: */
125         GtkComboBox *combobox = GTK_COMBO_BOX (self);
126         gtk_combo_box_set_model (combobox, priv->model);
127
128         /* Serversecurity column:
129          * The ID model column in not shown in the view. */
130         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
131         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
132         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
133         "text", MODEL_COL_NAME, NULL);
134         
135         /* The application must call modest_serversecurity_combo_box_fill().
136          */
137 }
138
139 ModestServersecurityComboBox*
140 modest_serversecurity_combo_box_new (void)
141 {
142         return g_object_new (MODEST_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
143 }
144
145 /* Fill the combo box with appropriate choices.
146  * #combobox: The combo box.
147  * @protocol: IMAP or POP.
148  */
149 void modest_serversecurity_combo_box_fill (ModestServersecurityComboBox *combobox, ModestTransportStoreProtocol protocol)
150 {
151         ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
152         priv->protocol = protocol; /* Remembered for later. */
153         
154         /* Remove any existing rows: */
155         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
156         gtk_list_store_clear (liststore);
157         
158         GtkTreeIter iter;
159         gtk_list_store_append (liststore, &iter);
160         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_CONNECTION_NORMAL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
161         
162         /* Select the None item: */
163         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
164         
165         gtk_list_store_append (liststore, &iter);
166         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_CONNECTION_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
167         
168         /* Add security choices with protocol-specific names, as in the UI spec:
169          * (Note: Changing the title seems pointless. murrayc) */
170         if(protocol == MODEST_PROTOCOL_STORE_POP) {
171                 gtk_list_store_append (liststore, &iter);
172                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_CONNECTION_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
173         } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
174                 gtk_list_store_append (liststore, &iter);
175                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_CONNECTION_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
176         } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
177                 gtk_list_store_append (liststore, &iter);
178                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_CONNECTION_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
179         }
180 }
181
182 static gint get_port_for_security (ModestTransportStoreProtocol protocol, ModestConnectionProtocol security)
183 {
184         /* See the UI spec, section Email Wizards, Incoming Details [MSG-WIZ001]: */
185         gint result = 0;
186
187         /* Get the default port number for this protocol with this security: */
188         if(protocol == MODEST_PROTOCOL_STORE_POP) {
189                 if ((security ==  MODEST_PROTOCOL_CONNECTION_NORMAL) || (security ==  MODEST_PROTOCOL_CONNECTION_TLS))
190                         result = 110;
191                 else if (security ==  MODEST_PROTOCOL_CONNECTION_SSL)
192                         result = 995;
193         } else if (protocol == MODEST_PROTOCOL_STORE_IMAP) {
194                 if ((security ==  MODEST_PROTOCOL_CONNECTION_NORMAL) || (security ==  MODEST_PROTOCOL_CONNECTION_TLS))
195                         result = 143;
196                 else if (security ==  MODEST_PROTOCOL_CONNECTION_SSL)
197                         result = 993;
198         } else if (protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
199                 if ((security ==  MODEST_PROTOCOL_CONNECTION_NORMAL) || (security ==  MODEST_PROTOCOL_CONNECTION_TLS))
200                         result = 25;
201                 else if (security ==  MODEST_PROTOCOL_CONNECTION_SSL)
202                         result = 465;
203         }
204
205         return result;
206 }
207
208 /**
209  * Returns the selected serversecurity, 
210  * or MODEST_PROTOCOL_CONNECTION_NORMAL if no serversecurity was selected.
211  */
212 ModestConnectionProtocol
213 modest_serversecurity_combo_box_get_active_serversecurity (ModestServersecurityComboBox *combobox)
214 {
215         GtkTreeIter active;
216         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
217         if (found) {
218                 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
219
220                 ModestConnectionProtocol serversecurity = MODEST_PROTOCOL_CONNECTION_NORMAL;
221                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
222                 return serversecurity;  
223         }
224
225         return MODEST_PROTOCOL_CONNECTION_NORMAL; /* Failed. */
226 }
227
228 /**
229  * Returns the default port to be used for the selected serversecurity, 
230  * or 0 if no serversecurity was selected.
231  */
232 gint
233 modest_serversecurity_combo_box_get_active_serversecurity_port (ModestServersecurityComboBox *combobox)
234 {
235         ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
236         
237         const ModestConnectionProtocol security = modest_serversecurity_combo_box_get_active_serversecurity 
238                 (combobox);
239         return get_port_for_security (priv->protocol, security);
240 }
241         
242 /* This allows us to pass more than one piece of data to the signal handler,
243  * and get a result: */
244 typedef struct 
245 {
246                 ModestServersecurityComboBox* self;
247                 gint id;
248                 gboolean found;
249 } ForEachData;
250
251 static gboolean
252 on_model_foreach_select_id(GtkTreeModel *model, 
253         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
254 {
255         ForEachData *state = (ForEachData*)(user_data);
256         
257         /* Select the item if it has the matching ID: */
258         guint id = 0;
259         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
260         if(id == state->id) {
261                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
262                 
263                 state->found = TRUE;
264                 return TRUE; /* Stop walking the tree. */
265         }
266         
267         return FALSE; /* Keep walking the tree. */
268 }
269
270 /**
271  * Selects the specified serversecurity, 
272  * or MODEST_PROTOCOL_CONNECTION_NORMAL if no serversecurity was selected.
273  */
274 gboolean
275 modest_serversecurity_combo_box_set_active_serversecurity (ModestServersecurityComboBox *combobox,
276                                                            ModestConnectionProtocol serversecurity)
277 {
278         ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
279         
280         /* Create a state instance so we can send two items of data to the signal handler: */
281         ForEachData *state = g_new0 (ForEachData, 1);
282         state->self = combobox;
283         state->id = serversecurity;
284         state->found = FALSE;
285         
286         /* Look at each item, and select the one with the correct ID: */
287         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
288
289         const gboolean result = state->found;
290         
291         /* Free the state instance: */
292         g_free(state);
293         
294         return result;
295 }
296