103226ba11b65054d52f36b83f744a1e59a11bc8
[modest] / src / hildon2 / modest-secureauth-picker.c
1 /* Copyright (c) 2007, 2008, 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 #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>
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 (ModestSecureauthPicker, modest_secureauth_picker, HILDON_TYPE_PICKER_BUTTON);
46
47 #define MODEST_SECUREAUTH_PICKER_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_PICKER, ModestSecureauthPickerPrivate))
49
50 typedef struct _ModestSecureauthPickerPrivate ModestSecureauthPickerPrivate;
51
52 struct _ModestSecureauthPickerPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 modest_secureauth_picker_finalize (GObject *object)
59 {
60         ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (object);
61
62         g_object_unref (G_OBJECT (priv->model));
63
64         G_OBJECT_CLASS (modest_secureauth_picker_parent_class)->finalize (object);
65 }
66
67 static void
68 modest_secureauth_picker_class_init (ModestSecureauthPickerClass *klass)
69 {
70         GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72         g_type_class_add_private (klass, sizeof (ModestSecureauthPickerPrivate));
73
74         object_class->finalize = modest_secureauth_picker_finalize;
75 }
76
77 enum MODEL_COLS {
78         MODEL_COL_NAME = 0, /* a string */
79         MODEL_COL_ID = 1 /* an int. */
80 };
81
82 void modest_secureauth_picker_fill (ModestSecureauthPicker *picker);
83
84 static gchar *
85 touch_selector_print_func (HildonTouchSelector *selector)
86 {
87         GtkTreeIter iter;
88         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
89                 GtkTreeModel *model;
90                 GValue value = {0,};
91                 
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);
95         }
96         return NULL;
97 }
98
99 static void
100 modest_secureauth_picker_init (ModestSecureauthPicker *self)
101 {
102         ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (self);
103
104         priv->model = NULL;
105 }
106
107 ModestSecureauthPicker*
108 modest_secureauth_picker_new (void)
109 {
110         ModestSecureauthPicker *self;
111         ModestSecureauthPickerPrivate *priv;
112         GtkCellRenderer *renderer;
113         GtkWidget *selector;
114
115         self = g_object_new (MODEST_TYPE_SECUREAUTH_PICKER, 
116                              "arrangement", HILDON_BUTTON_ARRANGEMENT_VERTICAL,
117                              "size", HILDON_SIZE_AUTO,
118                              NULL);
119         priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (self);
120
121         /* Create a tree model,
122          * with a string for the name, and an ID for the servertype.
123          * This must match our MODEL_COLS enum constants.
124          */
125         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
126         renderer = gtk_cell_renderer_text_new ();
127         g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
128
129         selector = hildon_touch_selector_new ();
130         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
131                                              renderer, "text", MODEL_COL_NAME, NULL);
132
133         hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
134         hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
135
136         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
137
138         modest_secureauth_picker_fill (self);
139
140         return self;
141 }
142
143 /* Fill the combo box with appropriate choices.
144  * #picker: The combo box.
145  * @protocol: IMAP or POP.
146  */
147 void modest_secureauth_picker_fill (ModestSecureauthPicker *picker)
148 {       
149         ModestSecureauthPickerPrivate *priv;
150         GtkListStore *liststore;
151         ModestProtocolRegistry *protocol_registry;
152         GSList *protocols, *node;
153         GtkTreeIter iter;
154
155         priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
156         
157         /* Remove any existing rows: */
158         liststore = GTK_LIST_STORE (priv->model);
159         gtk_list_store_clear (liststore);
160
161         protocol_registry = modest_runtime_get_protocol_registry ();
162         protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
163
164         for (node = protocols; node != NULL; node = g_slist_next (node)) {
165                 ModestProtocol *protocol;
166                 protocol = (ModestProtocol *) node->data;
167
168                 gtk_list_store_append (liststore, &iter);
169                 gtk_list_store_set (liststore, &iter, 
170                                     MODEL_COL_ID, (gint)modest_protocol_get_type_id (protocol),
171                                     MODEL_COL_NAME, modest_protocol_get_display_name (protocol),
172                                     -1);
173         }       
174 }
175
176 /**
177  * Returns the selected secureauth, 
178  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
179  */
180 ModestProtocolType
181 modest_secureauth_picker_get_active_secureauth (ModestSecureauthPicker *picker)
182 {
183         GtkTreeIter active;
184         gboolean found;
185         GtkWidget *selector;
186
187         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
188
189         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
190         if (found) {
191                 ModestSecureauthPickerPrivate *priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
192
193                 ModestProtocolType secure_auth = MODEST_PROTOCOLS_AUTH_NONE;
194                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secure_auth, -1);
195                 return secure_auth;     
196         }
197
198         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
199 }
200
201 /* This allows us to pass more than one piece of data to the signal handler,
202  * and get a result: */
203 typedef struct 
204 {
205         ModestSecureauthPicker* self;
206         ModestProtocolType id;
207         gboolean found;
208 } ForEachData;
209
210 static gboolean
211 on_model_foreach_select_id(GtkTreeModel *model, 
212         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
213 {
214         ForEachData *state;
215         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
216
217         state = (ForEachData*)(user_data);
218         
219         /* Select the item if it has the matching ID: */
220         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
221         if(id == state->id) {
222                 GtkWidget *selector;
223                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
224                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
225                 hildon_button_set_value (HILDON_BUTTON (state->self),
226                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
227                 
228                 state->found = TRUE;
229                 return TRUE; /* Stop walking the tree. */
230         }
231         
232         return FALSE; /* Keep walking the tree. */
233 }
234
235 /**
236  * Selects the specified secureauth, 
237  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
238  */
239 gboolean
240 modest_secureauth_picker_set_active_secureauth (ModestSecureauthPicker *picker, ModestProtocolType secureauth)
241 {
242         ModestSecureauthPickerPrivate *priv;
243         ForEachData *state;
244         gboolean result;
245
246         priv = MODEST_SECUREAUTH_PICKER_GET_PRIVATE (picker);
247         
248         /* Create a state instance so we can send two items of data to the signal handler: */
249         state = g_new0 (ForEachData, 1);
250         state->self = picker;
251         state->id = secureauth;
252         state->found = FALSE;
253         
254         /* Look at each item, and select the one with the correct ID: */
255         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
256
257         result = state->found;
258         
259         /* Free the state instance: */
260         g_free(state);
261         
262         return result;
263 }
264