Avoid wait on taking screenshot. Now it shouldn't be required.
[modest] / src / hildon2 / modest-serversecurity-picker.c
1 /* Copyright (c) 2006, 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
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>
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 (ModestServersecurityPicker, modest_serversecurity_picker, HILDON_TYPE_PICKER_BUTTON);
48
49 #define MODEST_SERVERSECURITY_PICKER_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERSECURITY_PICKER, ModestServersecurityPickerPrivate))
51
52 typedef struct _ModestServersecurityPickerPrivate ModestServersecurityPickerPrivate;
53
54 struct _ModestServersecurityPickerPrivate
55 {
56         GtkTreeModel *model;
57         ModestProtocolType protocol;
58 };
59
60 static void
61 modest_serversecurity_picker_finalize (GObject *object)
62 {
63         ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66
67         G_OBJECT_CLASS (modest_serversecurity_picker_parent_class)->finalize (object);
68 }
69
70 static void
71 modest_serversecurity_picker_class_init (ModestServersecurityPickerClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (ModestServersecurityPickerPrivate));
76
77         object_class->finalize = modest_serversecurity_picker_finalize;
78 }
79
80 enum MODEL_COLS {
81         MODEL_COL_NAME = 0, /* a string */
82         MODEL_COL_ID = 1 /* an int. */
83 };
84
85 static void
86 modest_serversecurity_picker_init (ModestServersecurityPicker *self)
87 {
88         ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (self);
89
90         priv->model = NULL;
91
92 }
93
94 static gchar *
95 touch_selector_print_func (HildonTouchSelector *selector, gpointer userdata)
96 {
97         GtkTreeIter iter;
98         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
99                 GtkTreeModel *model;
100                 GValue value = {0,};
101                 
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);
105         }
106         return NULL;
107 }
108
109 ModestServersecurityPicker*
110 modest_serversecurity_picker_new (HildonSizeType size,
111                                   HildonButtonArrangement arrangement)
112 {
113         ModestServersecurityPicker *self;
114         ModestServersecurityPickerPrivate *priv;
115         GtkCellRenderer *renderer;
116         GtkWidget *selector;
117
118         self = g_object_new (MODEST_TYPE_SERVERSECURITY_PICKER, 
119                              "arrangement", arrangement,
120                              "size", size,
121                              NULL);
122         priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (self);
123
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.
127          */
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);
131
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);
135
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), (HildonTouchSelectorPrintFunc) touch_selector_print_func);
138
139         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
140
141         /* For theming purpouses. Widget name must end in Button-finger */
142         gtk_widget_set_name ((GtkWidget *) self, "ModestServersecurityPickerButton-finger");
143
144         return self;
145 }
146
147 /* Fill the picker box with appropriate choices.
148  * #picker: The picker box.
149  * @protocol: IMAP or POP.
150  */
151 void modest_serversecurity_picker_fill (ModestServersecurityPicker *picker, ModestProtocolType protocol_type)
152 {
153         ModestServersecurityPickerPrivate *priv;
154         ModestProtocol *protocol;
155         GtkWidget *selector;
156
157         priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
158         priv->protocol = protocol_type; /* Remembered for later. */
159         protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
160                                                                   protocol_type);
161         
162         /* Remove any existing rows: */
163         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
164         gtk_list_store_clear (liststore);
165         
166         GtkTreeIter iter;
167         gtk_list_store_append (liststore, &iter);
168         /* TODO: This logical ID is not in the .po file: */
169         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint) MODEST_PROTOCOLS_CONNECTION_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
170         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
171         hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &iter, TRUE);
172         hildon_button_set_value (HILDON_BUTTON (picker), 
173                                  hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
174         
175         gtk_list_store_append (liststore, &iter);
176         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOLS_CONNECTION_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
177         
178         /* Add security choices with protocol-specific names, as in the UI spec:
179          * (Note: Changing the title seems pointless. murrayc) */
180         gchar *protocol_name = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_SSL_PROTO_NAME);
181         if (protocol_name) {
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, protocol_name, -1);
184                 g_free (protocol_name);
185         } else {
186                 /* generic fallback */
187                 gtk_list_store_append (liststore, &iter);
188                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOLS_CONNECTION_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
189         }
190 }
191
192 static gint get_port_for_security (ModestProtocolType protocol_type, ModestProtocolType security_type)
193 {
194         /* See the UI spec, section Email Wizards, Incoming Details [MSG-WIZ001]: */
195         gint result = 0;
196         ModestProtocol *protocol, *security;
197         ModestProtocolRegistry *protocol_registry;
198         gboolean use_alternate_port;
199
200         protocol_registry = modest_runtime_get_protocol_registry ();
201         protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
202         security = modest_protocol_registry_get_protocol_by_type (protocol_registry, security_type);
203
204         g_return_val_if_fail ((security != NULL && protocol != NULL), 0);
205
206         use_alternate_port = modest_protocol_registry_protocol_type_has_tag (protocol_registry, security_type,
207                                                                              MODEST_PROTOCOL_REGISTRY_USE_ALTERNATE_PORT);
208
209         /* Get the default port number for this protocol with this security: */
210         if (use_alternate_port) {
211                 result = modest_account_protocol_get_alternate_port (MODEST_ACCOUNT_PROTOCOL (protocol));
212         } else {
213                 result = modest_account_protocol_get_port (MODEST_ACCOUNT_PROTOCOL (protocol));
214         }
215
216         return result;
217 }
218
219 /**
220  * Returns the selected serversecurity, 
221  * or MODEST_PROTOCOLS_CONNECTION_NONE if no serversecurity was selected.
222  */
223 ModestProtocolType
224 modest_serversecurity_picker_get_active_serversecurity (ModestServersecurityPicker *picker)
225 {
226         GtkTreeIter active;
227         gboolean found;
228         GtkWidget *selector;
229
230         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
231
232         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
233         if (found) {
234                 ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
235
236                 ModestProtocolType serversecurity = MODEST_PROTOCOLS_CONNECTION_NONE;
237                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
238                 return serversecurity;  
239         }
240
241         return MODEST_PROTOCOLS_CONNECTION_NONE; /* Failed. */
242 }
243
244 /**
245  * Returns the default port to be used for the selected serversecurity, 
246  * or 0 if no serversecurity was selected.
247  */
248 gint
249 modest_serversecurity_picker_get_active_serversecurity_port (ModestServersecurityPicker *picker)
250 {
251         ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
252         
253         ModestProtocolType security = modest_serversecurity_picker_get_active_serversecurity 
254                 (picker);
255         return get_port_for_security (priv->protocol, security);
256 }
257         
258 /* This allows us to pass more than one piece of data to the signal handler,
259  * and get a result: */
260 typedef struct 
261 {
262                 ModestServersecurityPicker* self;
263                 ModestProtocolType id;
264                 gboolean found;
265 } ForEachData;
266
267 static gboolean
268 on_model_foreach_select_id(GtkTreeModel *model, 
269         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
270 {
271         ForEachData *state = (ForEachData*)(user_data);
272         
273         /* Select the item if it has the matching ID: */
274         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
275         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
276         if(id == state->id) {
277                 GtkWidget *selector;
278                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
279                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
280                 hildon_button_set_value (HILDON_BUTTON (state->self),
281                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
282                 
283                 state->found = TRUE;
284                 return TRUE; /* Stop walking the tree. */
285         }
286         
287         return FALSE; /* Keep walking the tree. */
288 }
289
290 /**
291  * Selects the specified serversecurity, 
292  * or MODEST_PROTOCOLS_CONNECTION_NONE if no serversecurity was selected.
293  */
294 gboolean
295 modest_serversecurity_picker_set_active_serversecurity (ModestServersecurityPicker *picker,
296                                                         ModestProtocolType serversecurity)
297 {
298         ModestServersecurityPickerPrivate *priv = MODEST_SERVERSECURITY_PICKER_GET_PRIVATE (picker);
299         
300         /* Create a state instance so we can send two items of data to the signal handler: */
301         ForEachData *state = g_new0 (ForEachData, 1);
302         state->self = picker;
303         state->id = serversecurity;
304         state->found = FALSE;
305         
306         /* Look at each item, and select the one with the correct ID: */
307         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
308
309         const gboolean result = state->found;
310         
311         /* Free the state instance: */
312         g_free(state);
313         
314         return result;
315 }
316