1 /* Copyright (c) 2007, 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.
30 #include "modest-secureauth-picker.h"
31 #include <modest-runtime.h>
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.h>
38 #include <string.h> /* For memcpy() */
40 /* Include config.h so that _() works: */
45 G_DEFINE_TYPE (ModestSecureauthPicker, modest_secureauth_picker, HILDON_TYPE_PICKER_BUTTON);
47 #define MODEST_SECUREAUTH_PICKER_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_PICKER, ModestSecureauthPickerPrivate))
50 typedef struct _ModestSecureauthPickerPrivate ModestSecureauthPickerPrivate;
52 struct _ModestSecureauthPickerPrivate
58 modest_secureauth_picker_finalize (GObject *object)
60 ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (modest_secureauth_picker_parent_class)->finalize (object);
68 modest_secureauth_picker_class_init (ModestSecureauthPickerClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (ModestSecureauthPickerPrivate));
74 object_class->finalize = modest_secureauth_picker_finalize;
78 MODEL_COL_NAME = 0, /* a string */
79 MODEL_COL_ID = 1 /* an int. */
82 static void modest_secureauth_picker_fill (ModestSecureauthPicker *picker);
85 touch_selector_print_func (HildonTouchSelector *selector, gpointer userdata)
88 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
92 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
93 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
94 return g_value_dup_string (&value);
100 modest_secureauth_picker_init (ModestSecureauthPicker *self)
102 ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (self);
107 ModestSecureauthPicker*
108 modest_secureauth_picker_new (HildonSizeType size,
109 HildonButtonArrangement arrangement)
111 ModestSecureauthPicker *self;
112 ModestSecureauthPickerPrivate *priv;
113 GtkCellRenderer *renderer;
116 self = g_object_new (MODEST_TYPE_SECUREAUTH_PICKER,
117 "arrangement", arrangement,
120 priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (self);
122 /* Create a tree model,
123 * with a string for the name, and an ID for the servertype.
124 * This must match our MODEL_COLS enum constants.
126 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
127 renderer = gtk_cell_renderer_text_new ();
128 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
130 selector = hildon_touch_selector_new ();
131 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
132 renderer, "text", MODEL_COL_NAME, NULL);
134 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
135 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), (HildonTouchSelectorPrintFunc) touch_selector_print_func);
137 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
139 modest_secureauth_picker_fill (self);
144 /* Fill the combo box with appropriate choices.
145 * #picker: The combo box.
146 * @protocol: IMAP or POP.
149 modest_secureauth_picker_fill (ModestSecureauthPicker *picker)
151 ModestSecureauthPickerPrivate *priv;
152 GtkListStore *liststore;
153 ModestProtocolRegistry *protocol_registry;
154 GSList *protocols, *node;
157 priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
159 /* Remove any existing rows: */
160 liststore = GTK_LIST_STORE (priv->model);
161 gtk_list_store_clear (liststore);
163 protocol_registry = modest_runtime_get_protocol_registry ();
164 protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
166 for (node = protocols; node != NULL; node = g_slist_next (node)) {
167 ModestProtocol *protocol;
168 protocol = (ModestProtocol *) node->data;
170 gtk_list_store_append (liststore, &iter);
171 gtk_list_store_set (liststore, &iter,
172 MODEL_COL_ID, (gint)modest_protocol_get_type_id (protocol),
173 MODEL_COL_NAME, modest_protocol_get_display_name (protocol),
175 if (modest_protocol_get_type_id (protocol) == MODEST_PROTOCOLS_AUTH_NONE) {
176 HildonTouchSelector *selector;
177 selector = hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker));
178 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &iter, TRUE);
179 hildon_button_set_value (HILDON_BUTTON (picker),
180 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
187 * Returns the selected secureauth,
188 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
191 modest_secureauth_picker_get_active_secureauth (ModestSecureauthPicker *picker)
197 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
199 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
201 ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
203 ModestProtocolType secure_auth = MODEST_PROTOCOLS_AUTH_NONE;
204 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secure_auth, -1);
208 return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
211 /* This allows us to pass more than one piece of data to the signal handler,
212 * and get a result: */
215 ModestSecureauthPicker* self;
216 ModestProtocolType id;
221 on_model_foreach_select_id(GtkTreeModel *model,
222 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
225 ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
227 state = (ForEachData*)(user_data);
229 /* Select the item if it has the matching ID: */
230 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
231 if(id == state->id) {
233 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
234 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
235 hildon_button_set_value (HILDON_BUTTON (state->self),
236 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
239 return TRUE; /* Stop walking the tree. */
242 return FALSE; /* Keep walking the tree. */
246 * Selects the specified secureauth,
247 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
250 modest_secureauth_picker_set_active_secureauth (ModestSecureauthPicker *picker, ModestProtocolType secureauth)
252 ModestSecureauthPickerPrivate *priv;
256 priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
258 /* Create a state instance so we can send two items of data to the signal handler: */
259 state = g_new0 (ForEachData, 1);
260 state->self = picker;
261 state->id = secureauth;
262 state->found = FALSE;
264 /* Look at each item, and select the one with the correct ID: */
265 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
267 result = state->found;
269 /* Free the state instance: */