ceb522ed82b63b139bb57d617d892bbe0eca81e4
[modest] / src / hildon2 / modest-servertype-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 #include <modest-runtime.h>
31 #include "modest-servertype-picker.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 (ModestServertypePicker, modest_servertype_picker, HILDON_TYPE_PICKER_BUTTON);
46
47 #define MODEST_SERVERTYPE_PICKER_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERTYPE_PICKER, ModestServertypePickerPrivate))
49
50 typedef struct _ModestServertypePickerPrivate ModestServertypePickerPrivate;
51
52 struct _ModestServertypePickerPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 modest_servertype_picker_finalize (GObject *object)
59 {
60         ModestServertypePickerPrivate *priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (object);
61
62         g_object_unref (G_OBJECT (priv->model));
63
64         G_OBJECT_CLASS (modest_servertype_picker_parent_class)->finalize (object);
65 }
66
67 static void
68 modest_servertype_picker_class_init (ModestServertypePickerClass *klass)
69 {
70         GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72         g_type_class_add_private (klass, sizeof (ModestServertypePickerPrivate));
73
74         object_class->finalize = modest_servertype_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 static void
83 modest_servertype_picker_init (ModestServertypePicker *self)
84 {
85         ModestServertypePickerPrivate *priv;
86
87         priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
88         priv->model = NULL;
89
90 }
91
92 static gchar *
93 touch_selector_print_func (HildonTouchSelector *selector)
94 {
95         GtkTreeIter iter;
96         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
97                 GtkTreeModel *model;
98                 GValue value = {0,};
99                 
100                 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
101                 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
102                 return g_value_dup_string (&value);
103         }
104         return NULL;
105 }
106
107
108 static void 
109 modest_servertype_picker_fill (ModestServertypePicker *self,
110                                      gboolean filter_providers)
111 {       
112         ModestServertypePickerPrivate *priv;
113         GtkListStore *liststore;
114         ModestProtocolRegistry *protocol_registry;
115         GSList *remote_protocols, *node;
116         GtkTreeIter iter;
117         GtkWidget *selector;
118         
119         /* Remove any existing rows: */
120         priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
121         protocol_registry = modest_runtime_get_protocol_registry ();
122         remote_protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS);
123
124         liststore = GTK_LIST_STORE (priv->model);
125         gtk_list_store_clear (liststore);
126
127         for (node = remote_protocols; node != NULL; node = g_slist_next (node)) {
128                 ModestProtocol* protocol;
129                 gboolean add = TRUE;
130
131                 protocol = (ModestProtocol *) node->data;
132
133                 /* Do not include the protocols that would be listed
134                    in the providers combo */
135                 if (filter_providers)
136                         if (modest_protocol_registry_protocol_type_is_provider (protocol_registry,
137                                                                                 modest_protocol_get_type_id (protocol))) {
138                                 add = FALSE;
139                         }
140                 
141                 if (add) {
142                         gtk_list_store_append (liststore, &iter);
143                         gtk_list_store_set (liststore, &iter, 
144                                             MODEL_COL_ID, 
145                                             modest_protocol_get_type_id (protocol),
146                                             MODEL_COL_NAME, 
147                                             modest_protocol_get_display_name (protocol),
148                                             -1);
149                 }
150         }
151         
152         g_slist_free (remote_protocols);
153
154         /* Choose first in list */
155         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
156         if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->model), &iter)) {
157                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &iter, TRUE);
158         }
159 }
160
161 ModestServertypePicker*
162 modest_servertype_picker_new (HildonSizeType size,
163                               HildonButtonArrangement arrangement,
164                               gboolean filter_providers)
165 {
166         ModestServertypePicker *self;
167         ModestServertypePickerPrivate *priv;
168         GtkCellRenderer *renderer;
169         GtkWidget *selector;
170
171         self = g_object_new (MODEST_TYPE_SERVERTYPE_PICKER, 
172                              "arrangement", arrangement,
173                              "size", size,
174                              NULL);
175         priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
176
177         /* Create a tree model,
178          * with a string for the name, and an ID for the servertype.
179          * This must match our MODEL_COLS enum constants.
180          */
181         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
182         renderer = gtk_cell_renderer_text_new ();
183         g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
184
185         selector = hildon_touch_selector_new ();
186         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
187                                              renderer, "text", MODEL_COL_NAME, NULL);
188
189         hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
190         hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
191
192         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
193
194         /* Fill the model */    
195         modest_servertype_picker_fill (self, filter_providers);
196
197         return self;
198 }
199
200 /**
201  * Returns the selected servertype, 
202  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no servertype was selected.
203  */
204 ModestProtocolType
205 modest_servertype_picker_get_active_servertype (ModestServertypePicker *self)
206 {
207         GtkTreeIter active;
208         gboolean found;
209         GtkWidget *selector;
210
211         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
212         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
213         if (found) {
214                 ModestServertypePickerPrivate *priv;
215                 ModestProtocolType servertype;
216
217                 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
218
219                 servertype = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
220                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
221                 return servertype;      
222         }
223
224         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
225 }
226
227 /* This allows us to pass more than one piece of data to the signal handler,
228  * and get a result: */
229 typedef struct 
230 {
231                 ModestServertypePicker* self;
232                 ModestProtocolType id;
233                 gboolean found;
234 } ForEachData;
235
236 static gboolean
237 on_model_foreach_select_id(GtkTreeModel *model, 
238         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
239 {
240         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
241         ForEachData *state = (ForEachData*)(user_data);
242         GtkWidget *selector;
243         
244         /* Select the item if it has the matching ID: */
245         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
246         if(id == state->id) {
247                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
248                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
249                 hildon_button_set_value (HILDON_BUTTON (state->self),
250                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
251                 
252                 state->found = TRUE;
253                 return TRUE; /* Stop walking the tree. */
254         }
255         
256         return FALSE; /* Keep walking the tree. */
257 }
258
259 /**
260  * Selects the specified servertype, 
261  * or MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN if no servertype was selected.
262  */
263 gboolean
264 modest_servertype_picker_set_active_servertype (ModestServertypePicker *picker, ModestProtocolType servertype)
265 {
266         ModestServertypePickerPrivate *priv;
267         ForEachData *state;
268         gboolean result;
269         
270         priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (picker);
271
272         /* Create a state instance so we can send two items of data to the signal handler: */
273         state = g_new0 (ForEachData, 1);
274         state->self = picker;
275         state->id = servertype;
276         state->found = FALSE;
277         
278         /* Look at each item, and select the one with the correct ID: */
279         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
280
281         result = state->found;
282         
283         /* Free the state instance: */
284         g_free(state);
285         
286         return result;
287 }
288