Modified webpage: now tinymail repository is in gitorious.
[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, gpointer userdata)
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), (HildonTouchSelectorPrintFunc) 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         /* For theming purpouses. Widget name must end in Button-finger */
198         gtk_widget_set_name ((GtkWidget *) self, "ModestServertypePickerButton-finger");
199
200         return self;
201 }
202
203 /**
204  * Returns the selected servertype, 
205  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no servertype was selected.
206  */
207 ModestProtocolType
208 modest_servertype_picker_get_active_servertype (ModestServertypePicker *self)
209 {
210         GtkTreeIter active;
211         gboolean found;
212         GtkWidget *selector;
213
214         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
215         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
216         if (found) {
217                 ModestServertypePickerPrivate *priv;
218                 ModestProtocolType servertype;
219
220                 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
221
222                 servertype = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
223                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
224                 return servertype;      
225         }
226
227         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
228 }
229
230 /* This allows us to pass more than one piece of data to the signal handler,
231  * and get a result: */
232 typedef struct 
233 {
234                 ModestServertypePicker* self;
235                 ModestProtocolType id;
236                 gboolean found;
237 } ForEachData;
238
239 static gboolean
240 on_model_foreach_select_id(GtkTreeModel *model, 
241         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
242 {
243         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
244         ForEachData *state = (ForEachData*)(user_data);
245         GtkWidget *selector;
246         
247         /* Select the item if it has the matching ID: */
248         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
249         if(id == state->id) {
250                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
251                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
252                 hildon_button_set_value (HILDON_BUTTON (state->self),
253                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
254                 
255                 state->found = TRUE;
256                 return TRUE; /* Stop walking the tree. */
257         }
258         
259         return FALSE; /* Keep walking the tree. */
260 }
261
262 /**
263  * Selects the specified servertype, 
264  * or MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN if no servertype was selected.
265  */
266 gboolean
267 modest_servertype_picker_set_active_servertype (ModestServertypePicker *picker, ModestProtocolType servertype)
268 {
269         ModestServertypePickerPrivate *priv;
270         ForEachData *state;
271         gboolean result;
272         
273         priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (picker);
274
275         /* Create a state instance so we can send two items of data to the signal handler: */
276         state = g_new0 (ForEachData, 1);
277         state->self = picker;
278         state->id = servertype;
279         state->found = FALSE;
280         
281         /* Look at each item, and select the one with the correct ID: */
282         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
283
284         result = state->found;
285         
286         /* Free the state instance: */
287         g_free(state);
288         
289         return result;
290 }
291