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