1 /* Copyright (c) 2006, 2008 Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #include "modest-serversecurity-picker.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>
40 #include <string.h> /* For memcpy() */
42 /* Include config.h so that _() works: */
47 G_DEFINE_TYPE (ModestServersecurityPicker, modest_serversecurity_picker, HILDON_TYPE_PICKER_BUTTON);
49 #define MODEST_SERVERSECURITY_PICKER_GET_PRIVATE(o) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERSECURITY_PICKER, ModestServersecurityPickerPrivate))
52 typedef struct _ModestServersecurityPickerPrivate ModestServersecurityPickerPrivate;
54 struct _ModestServersecurityPickerPrivate
57 ModestProtocolType protocol;
61 modest_serversecurity_picker_finalize (GObject *object)
63 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (object);
65 g_object_unref (G_OBJECT (priv->model));
67 G_OBJECT_CLASS (modest_serversecurity_picker_parent_class)->finalize (object);
71 modest_serversecurity_picker_class_init (ModestServersecurityPickerClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 g_type_class_add_private (klass, sizeof (ModestServersecurityPickerPrivate));
77 object_class->finalize = modest_serversecurity_picker_finalize;
81 MODEL_COL_NAME = 0, /* a string */
82 MODEL_COL_ID = 1 /* an int. */
86 modest_serversecurity_picker_init (ModestServersecurityPicker *self)
88 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (self);
95 touch_selector_print_func (HildonTouchSelector *selector)
98 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
102 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
103 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
104 return g_value_dup_string (&value);
109 ModestServersecurityPicker*
110 modest_serversecurity_picker_new (HildonSizeType size,
111 HildonButtonArrangement arrangement)
113 ModestServersecurityPicker *self;
114 ModestServersecurityPickerPrivate *priv;
115 GtkCellRenderer *renderer;
118 self = g_object_new (MODEST_TYPE_SERVERSECURITY_PICKER,
119 "arrangement", arrangement,
122 priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (self);
124 /* Create a tree model,
125 * with a string for the name, and an ID for the servertype.
126 * This must match our MODEL_COLS enum constants.
128 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
129 renderer = gtk_cell_renderer_text_new ();
130 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
132 selector = hildon_touch_selector_new ();
133 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
134 renderer, "text", MODEL_COL_NAME, NULL);
136 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
137 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
139 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
144 /* Fill the picker box with appropriate choices.
145 * #picker: The picker box.
146 * @protocol: IMAP or POP.
148 void modest_serversecurity_picker_fill (ModestServersecurityPicker *picker, ModestProtocolType protocol_type)
150 ModestServersecurityPickerPrivate *priv;
151 ModestProtocol *protocol;
153 priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
154 priv->protocol = protocol_type; /* Remembered for later. */
155 protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
158 /* Remove any existing rows: */
159 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
160 gtk_list_store_clear (liststore);
163 gtk_list_store_append (liststore, &iter);
164 /* TODO: This logical ID is not in the .po file: */
165 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint) MODEST_PROTOCOLS_CONNECTION_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
167 gtk_list_store_append (liststore, &iter);
168 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOLS_CONNECTION_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
170 /* Add security choices with protocol-specific names, as in the UI spec:
171 * (Note: Changing the title seems pointless. murrayc) */
172 gchar *protocol_name = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_SSL_PROTO_NAME);
174 gtk_list_store_append (liststore, &iter);
175 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOLS_CONNECTION_SSL, MODEL_COL_NAME, protocol_name, -1);
176 g_free (protocol_name);
178 /* generic fallback */
179 gtk_list_store_append (liststore, &iter);
180 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 static gint get_port_for_security (ModestProtocolType protocol_type, ModestProtocolType security_type)
186 /* See the UI spec, section Email Wizards, Incoming Details [MSG-WIZ001]: */
188 ModestProtocol *protocol, *security;
189 ModestProtocolRegistry *protocol_registry;
190 gboolean use_alternate_port;
192 protocol_registry = modest_runtime_get_protocol_registry ();
193 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
194 security = modest_protocol_registry_get_protocol_by_type (protocol_registry, security_type);
196 g_return_val_if_fail ((security != NULL && protocol != NULL), 0);
198 use_alternate_port = modest_protocol_registry_protocol_type_has_tag (protocol_registry, security_type,
199 MODEST_PROTOCOL_REGISTRY_USE_ALTERNATE_PORT);
201 /* Get the default port number for this protocol with this security: */
202 if (use_alternate_port) {
203 result = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
205 result = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
212 * Returns the selected serversecurity,
213 * or MODEST_PROTOCOLS_CONNECTION_NONE if no serversecurity was selected.
216 modest_serversecurity_picker_get_active_serversecurity (ModestServersecurityPicker *picker)
222 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
224 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
226 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
228 ModestProtocolType serversecurity = MODEST_PROTOCOLS_CONNECTION_NONE;
229 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
230 return serversecurity;
233 return MODEST_PROTOCOLS_CONNECTION_NONE; /* Failed. */
237 * Returns the default port to be used for the selected serversecurity,
238 * or 0 if no serversecurity was selected.
241 modest_serversecurity_picker_get_active_serversecurity_port (ModestServersecurityPicker *picker)
243 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
245 ModestProtocolType security = modest_serversecurity_picker_get_active_serversecurity
247 return get_port_for_security (priv->protocol, security);
250 /* This allows us to pass more than one piece of data to the signal handler,
251 * and get a result: */
254 ModestServersecurityPicker* self;
255 ModestProtocolType id;
260 on_model_foreach_select_id(GtkTreeModel *model,
261 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
263 ForEachData *state = (ForEachData*)(user_data);
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) {
270 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
271 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
272 hildon_button_set_value (HILDON_BUTTON (state->self),
273 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
276 return TRUE; /* Stop walking the tree. */
279 return FALSE; /* Keep walking the tree. */
283 * Selects the specified serversecurity,
284 * or MODEST_PROTOCOLS_CONNECTION_NONE if no serversecurity was selected.
287 modest_serversecurity_picker_set_active_serversecurity (ModestServersecurityPicker *picker,
288 ModestProtocolType serversecurity)
290 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
292 /* Create a state instance so we can send two items of data to the signal handler: */
293 ForEachData *state = g_new0 (ForEachData, 1);
294 state->self = picker;
295 state->id = serversecurity;
296 state->found = FALSE;
298 /* Look at each item, and select the one with the correct ID: */
299 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
301 const gboolean result = state->found;
303 /* Free the state instance: */